package br.com.californiahotel.server.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import br.com.californiahotel.client.service.ServicoService;
import br.com.californiahotel.server.hibernate.DatabaseUtils;
import br.com.californiahotel.server.hibernate.models.Servico;
import br.com.californiahotel.server.utils.Conversor;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class ServicoServiceImpl extends RemoteServiceServlet implements
ServicoService {

	private static final Class<Servico> SERVICO_CLASS = br.com.californiahotel.server.hibernate.models.Servico.class;
	private static final DatabaseUtils<Servico> DATABASE = new DatabaseUtils<Servico>();
	private static final Conversor conversor = new Conversor();

	@Override
	public boolean save(br.com.californiahotel.shared.models.Servico model) {
		try {
			DATABASE.save((Servico)conversor.convertModelToHibernate( model, SERVICO_CLASS));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List<br.com.californiahotel.shared.models.Servico> findAll() {
		ArrayList<br.com.californiahotel.shared.models.Servico> servicoList = new ArrayList<br.com.californiahotel.shared.models.Servico>();
		List<Servico> findAll = DATABASE.findAll(SERVICO_CLASS);
		for (Servico servico : findAll) {
			servicoList.add((br.com.californiahotel.shared.models.Servico) conversor.convertHibernateToModel(servico, br.com.californiahotel.shared.models.Servico.class));
		}
		return servicoList;
	}

	@Override
	public boolean delete(String key) {
		try {
			DATABASE.removeById(SERVICO_CLASS, key);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean delete(Long key) {
		try {
			DATABASE.removeById(SERVICO_CLASS, key);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


}
